<script>

import { ElMessage } from 'element-plus'
// import md5 from 'md5';
// import axios from 'axios'
import { loginFn } from '@/api/user.js'
export default {
  name: 'LoginView',
  data() {
    return {
      adminname: '',
      password: ''
    }
  },
  methods: {
    submitForm() {
      loginFn({
        adminname: this.adminname,
        password: this.password
      }).then(res => {
        // console.log(res);
        if (res.code === '10005') {
          // 未注册
          ElMessage.error(res.message)
          return
        } else if (res.code === '10003') {
          //密码错误
          ElMessage.error(res.message)
          return
        } else {
          //第一种修改数据的方法
          this.$store.commit('updateUserInfo',res.data)
          // 将token 存到本地
          localStorage.setItem('token',res.data.token)
          this.$router.push('/')
          // console.log(this.$router);
        }
      })
    },
  },
}
</script>


<template>
  <div class="login-container">
    <div class="form-wrap">
      <h2 class="header">乐购后台管理系统</h2>
      <el-input v-model="adminname" placeholder="请输入管理员账号" />
      <el-input class="psw" show-password type="password" v-model="password" @keyup.enter="submitForm" placeholder="请输入对应的密码" />
      <el-button type="primary" @click="submitForm">登录</el-button>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.login-container {
  height: 100%;
  background-color: rgb(230, 244, 244);

  .form-wrap {
    position: absolute;
    left: 50%;
    top: 50%;
    width: 360px;
    transform: translate(-50%, -50%);
    background-color: white;
    border-radius: 15px;
    padding: 32px 32px 20px;
  }

  .header {
    text-align: center;
    margin-bottom: 32px;
  }

  .psw {
    margin-top: 20px;
  }

  .el-button {
    width: 100%;
    margin-top: 20px;
  }
}
</style>